//
//  ExhibitionViewController.m
//  MeiLiProductModel
//
//  Created by B505 on 15/2/21.
//  Copyright (c) 2015年 孙晓林. All rights reserved.
//

#import "CollectionViewController.h"
#import "DRNRealTimeBlurView.h"
#import "SliderViewController.h"
#import "ShowsDetailViewController.h"
#import "PublicMethod.h"
#import "T4Marco.h"
#import "UIButton+Utils.h"
#import "UILabel+Utils.h"

#define NAVBAR_HEIGHT 44
#define MENU_BTN_X 12
#define MENU_BTN_Y 23
#define kCoverWidth 267
#define kCoverHeight 337
#define kYAxisHeight 100
#define kCollectBtnWidth 20
#define kShowNameLabelWidth 200
#define kShowNameLabelHeight 30
#define kStatusLabelWidth 120
#define kStatusLabelHeight 20
#define kRightBlurViewWidth 30
#define kLeftBlurViewWidth 237
#define kBlurViewHeight 65
#define kBlurViewYHeight 172
#define kLineWidth 1
#define kLineHeight 55
#define kEffectViewAlpha 0.25
#define kBottomImageHeight (SCREEN_HEIGHT - 557)

#define kTitleLabelWidth 100
#define kTitleLabelHeight 24

@interface CollectionViewController ()<UIScrollViewDelegate>
{
    NSArray *pictureArrray;
    NSArray *showsNameArray;
    NSArray *statusArray;
}
@property (weak, nonatomic) IBOutlet UIImageView *blurImageView;
@property (weak, nonatomic) IBOutlet UIView *effectView;
@property (weak, nonatomic) IBOutlet UIScrollView *bkdScrollView;
@property (strong, nonatomic) UIView *collectNavBar;

@property (nonatomic, assign) BOOL isCollected;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _collectNavBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NAVBAR_HEIGHT)];
    _collectNavBar.backgroundColor = [UIColor lightGrayColor];
    _collectNavBar.alpha = 0.5;
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(MENU_BTN_X, MENU_BTN_Y, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
    [menuBtn setImageWithName:@"btn_menu"];
    [menuBtn addTarget:self action:@selector(onClickLeftMenu:)];
    [_collectNavBar addSubview:menuBtn];
    [self.view addSubview:_collectNavBar];
    
    UILabel *titleLabel = [UILabel labelWithFrame:CGRectMake((SCREEN_WIDTH - 100) / 2, 10, kTitleLabelWidth, kTitleLabelHeight) font:[UIFont systemFontOfSize:16] textAlignment:NSTextAlignmentCenter textColor:[UIColor whiteColor] highlightedTextColor:[UIColor whiteColor]];
    titleLabel.text = @"收藏";
    [_collectNavBar addSubview:titleLabel];
    
    _isCollected = YES;
    
    _effectView.backgroundColor = [UIColor blackColor];
    _effectView.alpha = kEffectViewAlpha;
    
    pictureArrray = @[@"pic0.jpg", @"pic1.jpg",@"pic2.jpg"];
    showsNameArray = @[@"中央美术学院美术馆", @"中国国家美术馆", @"中央美术学院"];
    statusArray = @[@"3个展览进行中", @"2个展览进行中", @"1个展览进行中"];
    [self designTheViewForShowsCovew];
    
}
- (void)designTheViewForShowsCovew
{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    UIImage *blurImage = [PublicMethod blurryImage:[PublicMethod cutImage:[UIImage imageNamed:pictureArrray[0]] WithImageViewWidth:width WithImageViewHeight:height] withBlurLevel:0.155];
    _blurImageView.image = blurImage;
    _bkdScrollView.bounces = YES;
    _bkdScrollView.showsHorizontalScrollIndicator = NO;
    _bkdScrollView.showsVerticalScrollIndicator = NO;
    _bkdScrollView.alwaysBounceVertical = YES;
    _bkdScrollView.pagingEnabled = YES;
    _bkdScrollView.delegate = self;
    for (int i = 0; i < pictureArrray.count; i ++) {
        
        UIImage *picImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[i]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kCoverHeight];
        UIImageView *coverImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - kCoverWidth) / 2, kYAxisHeight + SCREEN_HEIGHT * i , kCoverWidth, kCoverHeight)];
        coverImageView.image = picImage;
        //shadow
        coverImageView.userInteractionEnabled=YES;//允许imageview接受touch
        coverImageView.layer.cornerRadius = 5.0;
        coverImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        coverImageView.layer.shadowOpacity = 0.9;
        coverImageView.layer.shadowRadius = 8;
        coverImageView.layer.shadowOffset = CGSizeMake(0, -3);
        
        coverImageView.tag = 1000 + i;
        [_bkdScrollView addSubview:coverImageView];
        
        //btn
        UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        collectBtn.frame = CGRectMake(coverImageView.frame.origin.x + kCoverWidth - kCollectBtnWidth * 2.5 - 14, coverImageView.frame.origin.y + 11, kCollectBtnWidth, kCollectBtnWidth);
        [collectBtn setImageWithName:@"home_collect"];
        collectBtn.tag = 100 + i;
        [collectBtn addTarget:self action:@selector(onClickCollect:)];
        
        UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        infoBtn.frame = CGRectMake(coverImageView.frame.origin.x + kCoverWidth - kCollectBtnWidth * 1.5, coverImageView.frame.origin.y + 11, kCollectBtnWidth, kCollectBtnWidth);
        [infoBtn setImageWithName:@"home_info"];
        infoBtn.tag = 200 + i;
        [infoBtn addTarget:self action:@selector(onClickInfo:)];
        [_bkdScrollView insertSubview:infoBtn aboveSubview:coverImageView];
        [_bkdScrollView insertSubview:collectBtn aboveSubview:coverImageView];
        
        //label
        UILabel *showsName = [UILabel labelWithFrame:CGRectMake((SCREEN_WIDTH - kShowNameLabelWidth) / 2, coverImageView.frame.origin.y + kCoverHeight + 25, kShowNameLabelWidth, kShowNameLabelHeight)
                                                font:[UIFont systemFontOfSize:21]
                                       textAlignment:NSTextAlignmentCenter
                                           textColor:HEXCOLOR(0xFFFFFF)
                                highlightedTextColor:HEXCOLOR(0xFFFFFF)];
        showsName.text = showsNameArray[i];
        [_bkdScrollView addSubview:showsName];
        
        UILabel *statusLabel = [UILabel labelWithFrame:CGRectMake((SCREEN_WIDTH - kStatusLabelWidth) / 2, showsName.frame.origin.y + kShowNameLabelHeight + 13, kStatusLabelWidth, kShowNameLabelHeight)
                                                  font:[UIFont systemFontOfSize:13]
                                         textAlignment:NSTextAlignmentCenter
                                             textColor:HEXCOLOR(0xFFFFFF)
                                  highlightedTextColor:HEXCOLOR(0xFFFFFF)];
        statusLabel.alpha = 0.5;
        statusLabel.text = statusArray[i];
        [_bkdScrollView addSubview:statusLabel];
        
        //spearor Line
        DRNRealTimeBlurView *leftBlurview = [[DRNRealTimeBlurView alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x, coverImageView.frame.origin.y + kBlurViewYHeight, kLeftBlurViewWidth, kBlurViewHeight)];
        leftBlurview.alpha = 0.95;
        UIImageView *lineImagView = [[UIImageView alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x + kLeftBlurViewWidth, coverImageView.frame.origin.y + kBlurViewYHeight + 5, kLineWidth, kLineHeight)];
        lineImagView.image = [UIImage imageNamed:@"home_line"];
        DRNRealTimeBlurView *rightBlurview = [[DRNRealTimeBlurView alloc]initWithFrame:CGRectMake(coverImageView.frame.origin.x + kLeftBlurViewWidth + 1, coverImageView.frame.origin.y + kBlurViewYHeight, kRightBlurViewWidth, kBlurViewHeight)];
        rightBlurview.alpha = 0.95;
        [_bkdScrollView addSubview:leftBlurview];
        [_bkdScrollView addSubview:lineImagView];
        [_bkdScrollView addSubview:rightBlurview];
        
        if (i < pictureArrray.count - 1) {
            
            UIImageView *bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - kCoverWidth) / 2, coverImageView.frame.origin.y + kCoverHeight + 120 , kCoverWidth, kBottomImageHeight)];
            bottomImageView.tag = 500 + i;
            [_bkdScrollView addSubview:bottomImageView];
        }
    }
    
    UIImage *bottomImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[1]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kBottomImageHeight];
    UIImageView *btmImageView = (UIImageView *)[_bkdScrollView viewWithTag:500];
    btmImageView.image = bottomImage;

}

- (void)onClickLeftMenu:(UIButton *)btn
{
    T4_LOG_FUNC();
    [[SliderViewController sharedSliderController] showLeftViewController];
    
}

- (void)onClickCollect:(UIButton *)btn
{
    T4_LOG_FUNC();
    [btn setSelected:_isCollected];
    _isCollected = !_isCollected;
    NSLog(@"%ld",btn.tag);
    
}

- (void)onClickInfo:(UIButton *)btn
{
    T4_LOG_FUNC();
    ShowsDetailViewController *detailView = [[ShowsDetailViewController alloc]init];
    [self presentViewController:detailView animated:YES completion:nil];
    
    /*switch (btn.tag) {
        case 200:
        {
            [self showWithClassName:@"ShowsDetailViewController" mode:T4ControllerAppendModePresent bundle:nil];
        }
            break;
            
        default:
            break;
    }*/
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _bkdScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * pictureArrray.count);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat yOffset = scrollView.contentOffset.y;
    int pageHeight = (int)self.view.frame.size.height;
    CGFloat changeAlpha = kEffectViewAlpha - (CGFloat)((int)yOffset % pageHeight) / pageHeight;
    CGFloat nextViewAlpha = (CGFloat)((int)yOffset % pageHeight) / pageHeight - kEffectViewAlpha;
    if (yOffset >= 0) {
        if (changeAlpha >= 0) {
            _effectView.alpha = changeAlpha;
        }else{
            if (nextViewAlpha <= 0.25) {
                _effectView.alpha = nextViewAlpha;
               
            }
            
        }
        
    }
    
    [UIView animateWithDuration:0.2
                     animations:^{
                         int index = yOffset / pageHeight;
                         CGFloat width = self.view.frame.size.width;
                         CGFloat height = self.view.frame.size.height;
                         UIImage *blurImage = [PublicMethod blurryImage:[PublicMethod cutImage:[UIImage imageNamed:pictureArrray[index]] WithImageViewWidth:width WithImageViewHeight:height] withBlurLevel:0.155];
                         _blurImageView.image = blurImage;
                         
                         if (index < pictureArrray.count - 1) {
                             UIImage *bottomImage = [PublicMethod cutImage:[UIImage imageNamed:pictureArrray[index + 1]] WithImageViewWidth:kCoverWidth WithImageViewHeight:kBottomImageHeight];
                             UIImageView *btmImageView = (UIImageView *)[_bkdScrollView viewWithTag:500 + index];
                             btmImageView.image = bottomImage;
                         }
                     }
                     completion:^(BOOL finished) {
                        
                     }];
    
     NSLog(@"%f",yOffset);
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
